﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Wpf_Examples.Views
{
    /// <summary>
    /// NumberFoldingCard.xaml 的交互逻辑
    /// </summary>
    public partial class NumberFoldingCard : Window
    {
        public NumberFoldingCard()
        {
            InitializeComponent();
            InitializeTimer();
            UpdateTime();
        }

        private DispatcherTimer _timer;

        private void InitializeTimer()
        {
            _timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(0.2)
            };
            _timer.Tick += Timer_Tick;
            _timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
             UpdateTime();
        }

        private void UpdateTime()
        {
            DateTime now = DateTime.Now;
            int currentHour = now.Hour;
            int currentMinute = now.Minute;
            int currentSecond = now.Second;

            UpdateNumber(HourCard, currentHour);
            UpdateNumber(MinuteCard, currentMinute);
            UpdateNumber(SecondCard, currentSecond);
        }

        private void UpdateNumber(UserControlLib.NumberFoldingCard hourCard, int currentValue)
        {
                if (hourCard.Number != currentValue.ToString() && !hourCard.IsFlipping)
                {
                    hourCard.NextNumber = currentValue.ToString ();
                    hourCard.StartFlip();
                }
        }
    }
}
